Run a Program as a Service

Want to run a program when the Raspberry Pi starts up? Or on a timer?

Sometimes it's useful to be able to start a program automatically when your Raspberry Pi starts up.

The most reliable way to do this is by creating a system service.


Services

Services are programs that run in the background on your computer. On the Raspberry Pi and most Linux systems, the tool that controls these services is called systemd. Once you set up a service, it is easy to start and stop it, see its status and even get it to run at particular times (e.g. when the computer starts)

There are a few steps required to set up the service. In the example below, we set up a service called myservice to run a python program called myprogram.

Create a Bash Script to Run the Program

First, you need to create a Bash script to run your program. Create a .sh file in /usr/bin:

cd /usr/bin/
sudo nano myservice.sh

Enter the following text, changing the text in yellow according to your needs:

DATE=`date '+%Y-%m-%d %H:%M:%S'`
echo "Myservice service started at ${DATE}" | systemd-cat -p info

python3 /home/pi/myprograms/myprogram.py

If you want to run 2 programs at the same time, you can use & to force the first one to run in the background:

python3 /home/pi/myprograms/myprogram.py &
python3 /home/pi/myprograms/myotherprogram.py

Use Ctrl-X to exit and save from the Nano editor.

Make it executable by running the following command:

sudo chmod +x /usr/bin/myservice.sh

Create a Unit File to Set up the Service

Now we will create a file that defines the service by telling systemd what program to run

sudo nano /etc/systemd/system/myservice.service

Enter the following:

[Unit]
Description=Myservice systemd service.

[Service]
Type=simple
User=pi
ExecStart=/bin/bash /usr/bin/myservice.sh

[Install]
WantedBy=multi-user.target

Start the Service

Run the following to start the service:

sudo systemctl start myservice

Check the Service

Run the following to check the status of the service:

sudo systemctl status myservice

Stop and Restart the Service

Run the following to check the stop the service:

sudo systemctl stop myservice

Or restart the service:

sudo systemctl restart myservice

Automatically Start the Service

If you want the service to start automatically when you boot up the Pi you can enable it:

sudo systemctl enable myservice

To stop it starting automatically you can disable it:

sudo systemctl disable myservice

Automatically Start the Service on a Timer

Note that sometimes the service won't start on reboot because it depends on something else that hasn't started yet. For example, if your service reads from a network drive, it won't work until the network drive is connected, which may take a few seconds after reboot. In these cases we can add a timer to delay the start of the service.

Create a .timer file matching the name of the .service file

sudo nano /etc/systemd/system/myservice.timer

Add the following:

[Unit]
Description=Load network drive with delay

[Timer]
OnBootSec=1min

[Install]
WantedBy=timers.target

Then enable the timer:

sudo systemctl enable myservice.timer